//
//  MyCar.m
//  coba-iOS
//
//  Created by 郭四虎 on 15/12/24.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "MyCar.h"

@implementation MyCar

+ (void)isBus:(void(^)(bool isbus))getBus
{
    NSURLSessionDataTask *operation = nil;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    //a.cooba.cc
    //c.com
    manager.requestSerializer.timeoutInterval = 10.f;
    operation = [manager GET:[NSString stringWithFormat:@"%@/macmark.json",CAR_URL] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
                NSLog(@"----%@",dic);
        
                if ([dic objectForKey:@"MAC"]) {
                    
                    [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"MAC"] forKey:@"MAC"];
                    [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"factory"] forKey:@"factory"];
                    
                    NSLog(@"%@ hahah %@",factory,MAC);
                    getBus(YES);
                }else{
                    getBus(NO);
                }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"MAC"];
        [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"factory"];
        getBus(NO);
        
    }];
}

+ (void)getMAC:(void(^)(void))success
{
    NSURLSessionDataTask *operation = nil;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    manager.requestSerializer.timeoutInterval = 60.f;
    NSString *url = [NSString stringWithFormat:@"http://192.168.84.1:2060/wifidog/exchange"];
    
    operation = [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"mac"] forKey:@"USERMAC"];
        [[NSUserDefaults standardUserDefaults]setObject:[dic objectForKey:@"remainFlow"] forKey:@"remainFlow"];
        NSLog(@"MAC    %@",USERMAC);
        NSLog(@"REMAINFLOW %f",[remainFlow floatValue]);
        success();
        //192.168.84.1:2060/wifidog/exchange?ip=192.168.84.39
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"MAC    %@",error);
    }];
}

@end
